﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Comdiv.QWeb.Files;

namespace Comdiv.QWeb.ViewEngine {
	public class QViewSourceDescriptor {
		public QViewSourceDescriptor(string name, IFileNameResolver fileresolver) {
			Name = name;
			Resolver = fileresolver;
			BuildFiles = new List<string>();
		}

		public string OutputDirectory { get; set; }
		public string[] Sources { get; set; }
		public string LibraryFile { get; set; }

		public List<string> BuildFiles { get; private set; }

		public string[] CSharpBuildFiles {
			get { return BuildFiles.Where(x => Path.GetExtension(x) == ".cs").ToArray(); }
		}

		public string[] EmbededResourcesFiles {
			get { return BuildFiles.Where(x => Path.GetExtension(x) != ".cs").ToArray(); }
		}

		public DateTime SourceVersion {
			get { return Sources.Select(File.GetLastWriteTime).Max(); }
		}

		public DateTime LibraryVersion {
			get {
				if (File.Exists(LibraryFile)) {
					return File.GetLastWriteTime(LibraryFile);
				}
				return DateTime.MinValue;
			}
		}

		public string[] CSharpSourceFiles {
			get { return Sources.Where(x => Path.GetExtension(x) == ".cs").ToArray(); }
		}

		public string[] VBxlSourceFiles {
			get { return Sources.Where(x => Path.GetExtension(x) == ".vbxl").ToArray(); }
		}

		public string[] ResourcesSourceFiles {
			get {
				return Sources.Where(x =>
				                     Path.GetExtension(x) == ".css" ||
				                     Path.GetExtension(x) == ".js" ||
				                     Path.GetExtension(x) == ".resx" ||
				                     Path.GetExtension(x) == ".embed"
					).ToArray();
			}
		}

		public bool NeedRecompile {
			get { return SourceVersion > LibraryVersion; }
		}


		public IFileNameResolver Resolver { get; set; }

		public string Name { get; set; }

		public void Reset() {
			OutputDirectory = Resolver.Resolve("~/tmp/qview/" + Name.Replace(".", "/"), false);
			Directory.CreateDirectory(OutputDirectory);
			var viewfiles = Resolver.ResolveAll(QWebContext.Current,
			                                    FileResolveResultType.FullPath,
			                                    FileNameResolverExtensions.DEFAULT_QVIEW_RESOLVE_PROBE_PATHS,
			                                    new[] {Name + ".*"},
			                                    null
				);
			IList<string> filteredfiles = new List<string>();
			IList<string> filteredfilenames = new List<string>();
			foreach (var f in viewfiles) {
				var name = Path.GetFileName(f);
				if (filteredfilenames.Contains(name)) continue;
				filteredfilenames.Add(name);
				filteredfiles.Add(f);
			}
			Sources = filteredfiles.ToArray();
			LibraryFile = Path.Combine(OutputDirectory, Name.Replace("/", "_") + "_view_lib_.dll");
		}

		public void CleanupOutput() {
			if (Directory.Exists(OutputDirectory)) {
				foreach (var file in Directory.GetFiles(OutputDirectory)) {
					File.Delete(file);
				}
			}
			Directory.CreateDirectory(OutputDirectory);
		}
	}
}